package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.Information;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.entities.Unit;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017-7-27.
 */
@Repository
public class InformationDaoImpl extends BaseDaoImpl<Information> {

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public Map<String, Object> get(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT " +
                " ti.*, tl. NAME labelName " +
                " FROM " +
                " t_information ti " +
                " LEFT JOIN t_label tl ON tl.id = ti.label_id " +
                " WHERE " +
                " ti.id = ?");
        arrList.add(id);
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }


    /**
     * 更新
     *
     * @param information
     * @return
     */
    public int editInfo(Information information) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_information set update_time = now(),updater = ?");
        arrList.add(information.getUpdater());
        if (!StringUtil.isEmpty(information.getTitle())) {
            sb.append(",title = ? ");
            arrList.add(information.getTitle());
        }
        if (!StringUtil.isEmpty(information.getAbstract_info())) {
            sb.append(",abstract_info = ? ");
            arrList.add(information.getAbstract_info());
        }
        if (!StringUtil.isEmpty(information.getContent())) {
            sb.append(",content = ? ");
            arrList.add(information.getContent());
        }
        if (information.getSource_url() != null) {
            sb.append(",source_url = ? ");
            arrList.add(information.getSource_url());
        }
        if (information.getPosition() != null) {
            sb.append(",position = ? ");
            arrList.add(information.getPosition());
        }
        if (information.getLabel_id() != null) {
            sb.append(",label_id = ? ");
            arrList.add(information.getLabel_id());
        }
        if (information.getIs_charge() != null) {
            sb.append(",is_charge = ? ");
            arrList.add(information.getIs_charge());
        }
        if (information.getMoney() != null) {
            sb.append(",money = ? ");
            arrList.add(information.getMoney());
        }
        if (information.getChannel_id() != null) {
            sb.append(",channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if (information.getColumn_id() != null) {
            sb.append(",column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        if (information.getPublish_status() != null) {
            sb.append(",publish_status = ? ");
            arrList.add(information.getPublish_status());
        }
        if (information.getPublish_time() != null) {
            sb.append(",publish_time = ? ");
            arrList.add(information.getPublish_time());
        }
        sb.append(",theme_img1 = ? ");
        arrList.add(information.getTheme_img1());
        sb.append(",theme_img2 = ? ");
        arrList.add(information.getTheme_img2());
        sb.append(",theme_img3 = ? ");
        arrList.add(information.getTheme_img3());
        sb.append(" where id = ?");
        arrList.add(information.getId());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int del(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_information set is_del = 1 WHERE id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 评论开关
     *
     * @param id
     * @return
     */
    public int toggleComment(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_information set is_comment = 1 - is_comment where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 获取列表
     *
     * @param pageBean
     * @return
     */
    public PageBean list(PageBean pageBean, Information information, boolean is_all) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT " +
                " tin.*, tl.name label_name,tu.unit_name,u.name creator_name,tc.channel_name channel_name,tco.column_name column_name " +
                " FROM " +
                " t_information tin " +
                " LEFT JOIN t_label tl ON tl.id = tin.label_id " +
                " LEFT JOIN t_unit tu ON tin.unit_id = tu.id" +
                " LEFT JOIN t_user u ON tin.creator = u.id" +
                " LEFT JOIN t_channel tc ON tin.channel_id = tc.id" +
                " LEFT JOIN t_column tco ON tin.column_id = tco.id" +
                " where tin.is_del = 0 ");
        if (information.getLabel_id() != null) {
            sb.append(" and tin.label_id = ? ");
            arrList.add(information.getLabel_id());
        }
        if (information.getChannel_id() != null) {
            sb.append(" and tin.channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if (information.getColumn_id() != null) {
            sb.append(" and tin.column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        if (information.getPublish_status() != null) {
            sb.append(" and tin.publish_status = ? ");
            arrList.add(information.getPublish_status());
        }
        if (information.getPosition() != null) {
            sb.append(" and tin.position = ? ");
            arrList.add(information.getPosition());
        }
        if (information.getIs_comment() != null) {
            sb.append(" and tin.is_comment = ? ");
            arrList.add(information.getIs_comment());
        }
        if (!StringUtil.isEmpty(information.getTitle())) {
            sb.append(" and tin.title like concat('%',?,'%') ");
            arrList.add(information.getTitle());
        }
        if (!StringUtil.isEmpty(information.getStartTime())) {
            sb.append(" and tin.create_time > ? ");
            arrList.add(information.getStartTime());
        }
        if (!StringUtil.isEmpty(information.getEndTime())) {
            sb.append(" and tin.create_time < ? ");
            arrList.add(information.getEndTime());
        }
        if (information.getPublish_time() != null) {
            sb.append(" and tin.publish_time = ? ");
            arrList.add(information.getPublish_time());
        }
        if(!is_all){
            dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "tu", "tin");
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        pageBean.setTotalPage(pageBean.getTotalPage());
        if (num > 0) {
            sb.append(" order by tin.id desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 改变阅读数
     *
     * @return
     */
    public int updateReading(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_information SET reading = reading + 1 WHERE id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新评论数
     * @param informationId
     */
    public void updateCommentNum(Integer informationId, Integer num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_information " +
                " SET comment_num = comment_num + ? " +
                " WHERE " +
                " id = ?");
        arrList.add(num);
        arrList.add(informationId);
        updateJdbc(sb.toString(), arrList);
    }


    /**
     *
     * @param channelId
     * @param columnId
     * @param isBanner
     * @return
     */
    public List listByChannelId(int channelId, int columnId, int isBanner){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*,t1.name label_name from t_information t left join t_label t1");
        sb.append(" on t1.id = t.label_id");
        sb.append(" where t.is_del = 0 and t1.is_del = 0 ");
        sb.append(" and t.publish_status = 3");
        sb.append(" and t.channel_id = ?");
        arrList.add(channelId);
        if(columnId!=0){
            sb.append(" and t.column_id = ?");
        }
        if(isBanner!=0){
            sb.append(" and t.position in (2,3,4)");
        }else{
            sb.append(" and t.position not in (2,3,4)");
        }
        sb.append(" order by position  ");

        sb.append(" limit 0,10");
        return getListJdbc(sb.toString(), arrList);
    }

    /**
     * 欢迎页面统计
     * @param unit
     * @return
     */
    public Map<String,Object> welcomeReport(Unit unit){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select IFNULL(count(1),0) articleNum,IFNULL(sum(reading),0) readNum from t_information t ");
        sb.append("join t_unit u on u.id = t.unit_id " );
        sb.append("where (u.id = ? or u.parent_ids like concat(?,'%')) and t.is_del = 0 ");
        arrList.add(unit.getId());
        arrList.add(unit.getParent_ids() + unit.getId() + ",");
        return getListJdbc(sb.toString(),arrList).get(0);
    }

}
